package org.lex.ftp.ui.find;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.lex.filter.Filter;
import org.lex.ftp.ui.find.filter.DateFilterPane;
import org.lex.ftp.ui.find.filter.FileTypeFilterPane;
import org.lex.ftp.ui.find.filter.GeneralPane;
import org.lex.ftp.ui.find.filter.SizeFilterPane;
import org.lex.net.ftp.IFTPFile;
import org.lex.swing.ContainerFactory;
import org.lex.swing.widgets.EnhancedDialog;


public class BaseFindDialog extends EnhancedDialog implements Filter<IFTPFile> {
	private JPanel pane = null;
	private JSplitPane mainSplitPane = null;
	private JScrollPane resultScrollPane = null;
	private JPanel controlPane = null;
	private JTable resultTable = null;
	private JTabbedPane queryTabbedPane = null;
	private JPanel queryControlPane = null;
	private GeneralPane generalPane = null;
	private DateFilterPane datePane = null;
	private SizeFilterPane sizePane = null;
	private FileTypeFilterPane miscPane = null;
	private JButton runButton = null;
	private JButton stopButton = null;
	private JButton closeButton = null;
	private JLabel statusLabel = null;
	private String defaultTitle = "Find";

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				BaseFindDialog thisClass = new BaseFindDialog(null);
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	public BaseFindDialog(Window parent) {
		super(parent);
		initialize();
	}

	public void setStatus(String status) {
		this.statusLabel.setText(status);
	}

	protected String getDefaultTitle() {
		return defaultTitle;
	}

	protected void setDefaultTitle(String defaultTitle) {
		this.defaultTitle = defaultTitle;
		this.setTitle(this.defaultTitle);
	}

	public boolean accept(IFTPFile file) {
		if (null == file)
			return false;
		if (false == this.miscPane.accept(file.isDirectory()))
			return false;
		if (false == this.datePane.accept(file.getDate()))
			return false;
		if (false == this.sizePane.accept(file.getSize()))
			return false;
		if (false == this.generalPane.accept(file.getName()))
			return false;
		return true;
	}

	protected void runButtonActionPerormed(ActionEvent e) {
	}

	protected void stopButtonActionPerformed(ActionEvent e) {
	}

	protected void closeButtonActionPerformed(ActionEvent e) {
		this.cancel();
	}

	public void cancel() {
		// int selection = JOptionPane.showConfirmDialog( this, "Do you really
		// want to close?",
		// "Closing", JOptionPane.YES_NO_CANCEL_OPTION,
		// JOptionPane.QUESTION_MESSAGE );
		// if( JOptionPane.YES_OPTION == selection )
		// {
		stopButtonActionPerformed(null);
		dispose();
		// }
	}

	// =======================
	private void initialize() {
		this.setSize(600, 450);
		this.setContentPane(getPane());
		this.setTitle(this.defaultTitle);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setDefaultButton(this.getRunButton());
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				cancel();
			}
		});
		this.getGeneralPane().getProgressBar().addChangeListener(new ChangeListener() {
			private JProgressBar pb = getGeneralPane().getProgressBar();
			private StringBuilder buf = new StringBuilder();

			public void stateChanged(ChangeEvent e) {
				this.refreshTitle();
			}

			private void refreshTitle() {
				buf.setLength(0);
				buf.append(getDefaultTitle());
				buf.append("  ( ");
				buf.append(pb.getValue());
				buf.append('/');
				buf.append(pb.getMaximum());
				buf.append(" )");
				setTitle(buf.toString());
			}
		});
	}

	private JPanel getPane() {
		if (pane == null) {
			statusLabel = new JLabel();
			statusLabel.setText("Status OK.");
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(getMainSplitPane(), BorderLayout.CENTER);
			pane.add(statusLabel, BorderLayout.SOUTH);
		}
		return pane;
	}

	private JSplitPane getMainSplitPane() {
		if (mainSplitPane == null) {
			mainSplitPane = ContainerFactory.createBorderlessSplitPane();
			mainSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			mainSplitPane.setDividerSize(5);
			mainSplitPane.setTopComponent(getControlPane());
			mainSplitPane.setBottomComponent(getResultScrollPane());
			mainSplitPane.setResizeWeight(0.01D);
		}
		return mainSplitPane;
	}

	private JScrollPane getResultScrollPane() {
		if (resultScrollPane == null) {
			resultScrollPane = new JScrollPane();
			resultScrollPane.setViewportView(getResultTable());
		}
		return resultScrollPane;
	}

	private JPanel getControlPane() {
		if (controlPane == null) {
			controlPane = new JPanel();
			controlPane.setLayout(new BorderLayout());
			controlPane.add(Box.createVerticalStrut(8), BorderLayout.NORTH);
			controlPane.add(Box.createVerticalStrut(5), BorderLayout.SOUTH);
			controlPane.add(Box.createHorizontalStrut(10), BorderLayout.WEST);
			controlPane.add(getQueryTabbedPane(), BorderLayout.CENTER);
			controlPane.add(getQueryControlPane(), BorderLayout.EAST);
		}
		return controlPane;
	}

	protected JTable getResultTable() {
		if (resultTable == null) {
			resultTable = new JTable();
		}
		return resultTable;
	}

	private JTabbedPane getQueryTabbedPane() {
		if (queryTabbedPane == null) {
			queryTabbedPane = new JTabbedPane();
			queryTabbedPane.addTab("General", null, getGeneralPane(), null);
			queryTabbedPane.addTab("Date", null, getDatePane(), null);
			queryTabbedPane.addTab("Size", null, getSizePane(), null);
			queryTabbedPane.addTab("Misc", null, getMiscPane(), null);
		}
		return queryTabbedPane;
	}

	private JPanel getQueryControlPane() {
		if (queryControlPane == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.insets = new Insets(0, 12, 11, 11);
			gridBagConstraints2.gridy = 2;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.gridx = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.insets = new Insets(0, 12, 11, 11);
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.insets = new Insets(12, 12, 11, 11);
			gridBagConstraints.gridy = 0;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.gridx = 0;
			queryControlPane = new JPanel();
			queryControlPane.setLayout(new GridBagLayout());
			queryControlPane.add(getRunButton(), gridBagConstraints);
			queryControlPane.add(getStopButton(), gridBagConstraints1);
			queryControlPane.add(getCloseButton(), gridBagConstraints2);
		}
		return queryControlPane;
	}

	protected GeneralPane getGeneralPane() {
		if (generalPane == null) {
			generalPane = new GeneralPane();
		}
		return generalPane;
	}

	protected DateFilterPane getDatePane() {
		if (datePane == null) {
			datePane = new DateFilterPane();
		}
		return datePane;
	}

	protected FileTypeFilterPane getMiscPane() {
		if (miscPane == null) {
			miscPane = new FileTypeFilterPane();
		}
		return miscPane;
	}

	protected JButton getRunButton() {
		if (runButton == null) {
			runButton = new JButton();
			runButton.setText("Run");
			runButton.setMnemonic(KeyEvent.VK_R);
			runButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					runButtonActionPerormed(e);
				}
			});
		}
		return runButton;
	}

	protected JButton getStopButton() {
		if (stopButton == null) {
			stopButton = new JButton();
			stopButton.setText("Stop");
			stopButton.setMnemonic(KeyEvent.VK_S);
			stopButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					stopButtonActionPerformed(e);
				}
			});
		}
		return stopButton;
	}

	protected JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setText("Close");
			closeButton.setMnemonic(KeyEvent.VK_C);
			closeButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					closeButtonActionPerformed(e);
				}
			});
		}
		return closeButton;
	}

	protected SizeFilterPane getSizePane() {
		if (sizePane == null) {
			sizePane = new SizeFilterPane();
		}
		return sizePane;
	}
}
